package pl.edu.utp.kursy.database.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import pl.edu.utp.kursy.database.Worker;

public class WorkerDao extends BaseDao<Worker>{
    
    public WorkerDao(SessionFactory sessionFactory){
        super(sessionFactory);
    }

    @Override
    public Worker getById(int id) {
        Session session = sessionFactory.openSession();
        Criteria c = session.createCriteria(Worker.class);
        c.add(Restrictions.eq("id", id));    		    		
        Worker worker = (Worker)c.uniqueResult();
        session.close(); 
        return worker; 
    }

    @Override
    public List<Worker> getAll() {
        ArrayList<Worker> workers;
        Session session = sessionFactory.openSession();
        Criteria c = session.createCriteria(Worker.class);
        c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        workers = (ArrayList<Worker>)c.list();
        session.close();
        return workers;  
    }
    
}
